/*
 *@description:产品相关接口
 *@Author:@rennan
 *@Date:2024-06-27 16:02:03
 *@version: V1.0.0
*/

const express = require('express');
const router = express.Router();

const sql = require('../db/index');

// 分页获取产品列表
/**
 * @swagger
 *    /api/product:
 *    get:
 *        description: 分页查询产品列表
 *        tags: [产品]
 *        parameters:
 *            - in: query
 *              name: page
 *              description: 页码
 *              required: true
 *              schema:
 *                type: number
 *            - in: query    
 *              name: pagesize
 *              description: 每页显示数量
 *              required: true
 *              schema:
 *                type: number
 *        responses:
 *          '200':
 *            description: 返回产品列表
 *            content:
 *              application/json:
 *                schema: 
 *                  $ref: '#/components/schemas/Product'
 */
router.get('/products',async(req,res)=>{
  //要分页，第一个查询那一页的数据，这一页数据有多少条 page = 1； pagesize=10
  let {page,pageSize} = req.query;
  let start = (page-1)*pageSize;
  let [products] = await sql.query(`SELECT * FROM jz_product limit ${start},${pageSize}`);
  res.send({
    code:200,
    message:'获取成功',
    data:products
  })
})



router.post('/products/saveOrUpdate', async function (req, res) {
    let { id, name, photo, product_category_id, description, price } = req.body;
    let mysql = '';
    // 判断id是否存在 存在修改 不存在新增
    if (id) {
    mysql = `update jz_product set name='${name}',
    photo='${photo}',description='${description}',product_category_id=${product_category_id},
    price='${price}' where id = ${id}`
    } else {
    mysql = `insert into jz_product
    (id,name,photo,status,description,product_category_id,price) values
    (null,'${name}','${photo}','${description}',${product_category_id},${price})`
    }
    await sql.query(mysql);
    res.send({
    status: 200,
    message: '操作成功',
    data: null,
    timestamp: new Date().getTime()
    })
    });

/**
 * @swagger
 *      /api/product/changeStatus:
 *      post:
 *          description: 上架和下架产品
 *          tags: [产品]
 *          parameters:
 *              - in: query
 *                name: id
 *                description: 产品id
 *                required: true
 *                schema:
 *                  type: number
 *              - in: query
 *                name: status
 *                description: 产品状态
 *                required: true
 *                schema:
 *                  type: string
 *          responses:
 *             '200':
 *                description: 返回产品列表
 */

router.post('/products/changeStatus', async function (req, res) {
    let { id, status } = req.body;
    if (status === '下架') {
    status = '下架'
    } else {
    status = '正常'
    }
    await sql.query(`update jz_product set status = '${status}' where id =${id}`);
    res.send({
    status:200,
    message:status=='下架'?'下架成功':'上架成功',
    data:null,
    timestamp:new Date().getTime()
    })
    })


    router.get('/products/pageQuery', async function (req, res) {
        let { page, pageSize, name, status, product_category_id } = req.query;
        page = +page;
        pageSize = +pageSize;
        product_category_id = +product_category_id;
        let result, result1;
        let offset = (page - 1) * pageSize;
        if (name && status && product_category_id) {
        let mysql = `select * from jz_product where name like '${'%' + name + '%'}' and
        status = '${status}' and product_category_id = ${product_category_id} limit ${pageSize}
        offset ${offset}`;
        result = await sql.query(mysql);
        let mysql1 = `select count(*) total from jz_product where name like '${'%' + name +
        '%'}' and status = '${status}' and product_category_id = ${product_category_id} limit
        ${pageSize} offset ${offset}`;
        result1 = await sql.query(mysql1);
        } else if (name && status) {
        let mysql = `select * from jz_product where name like '${'%' + name + '%'}' and
        status = '${status}' limit ${pageSize} offset ${offset}`;
        result = await sql.query(mysql);
        let mysql1 = `select count(*) total from jz_product where name like '${'%' + name +
        '%'}' and status = '${status}' limit ${pageSize} offset ${offset}`;
        result1 = await sql.query(mysql1);
        } else if (name && product_category_id) {
        let mysql = `select * from jz_product where name like '${'%' + name + '%'}' and
        product_category_id = ${product_category_id} limit ${pageSize} offset ${offset}`;
        result = await sql.query(mysql);
        let mysql1 = `select count(*) total from jz_product where name like '${'%' + name +
        '%'}' and product_category_id = ${product_category_id} limit ${pageSize} offset
        ${offset}`;
        result1 = await sql.query(mysql1);
        } else if (status && product_category_id) {
        let mysql = `select * from jz_product where status = '${status}' and
        product_category_id = ${product_category_id} limit ${pageSize} offset ${offset}`;
        result = await sql.query(mysql);
        let mysql1 = `select count(*) total from jz_product where status = '${status}' and
        product_category_id = ${product_category_id} limit ${pageSize} offset ${offset}`;
        result1 = await sql.query(mysql1);
        } else if (name || status || product_category_id) {
        if (name) {
        let mysql = `select * from jz_product where name like '${'%' + name + '%'}' limit
        ${pageSize} offset ${offset}`;
        result = await sql.query(mysql);
        let mysql1 = `select count(*) total from jz_product where name like '${'%' + name
        + '%'}' limit ${pageSize} offset ${offset}`;
        result1 = await sql.query(mysql1);
        } else if (status) {
        let mysql = `select * from jz_product where status ='${status}' limit ${pageSize}
        offset ${offset}`;
        result = await sql.query(mysql);
        let mysql1 = `select count(*) total from jz_product where status = '${status}'
limit ${pageSize} offset ${offset}`;
result1 = await sql.query(mysql1);
} else {
let mysql = `select * from jz_product where product_category_id =
${product_category_id} limit ${pageSize} offset ${offset}`;
result = await sql.query(mysql);
let mysql1 = `select count(*) total from jz_product where product_category_id =
${product_category_id} limit ${pageSize} offset ${offset}`;
result1 = await sql.query(mysql1);
}
} else {
let mysql = `select * from jz_product limit ${pageSize} offset ${offset}`;
result = await sql.query(mysql);
let mysql1 = `select count(*) total from jz_product limit ${pageSize} offset
${offset}`;
result1 = await sql.query(mysql1);
}
result = result.filter(item=>{
return item.length>0;
})
console.log(result[0],'2222');
res.send({
status: 200,
message: '查询成功',
data: result[0],
timestamp: new Date().getTime()
})
})

module.exports = router